<?php

/** 终端同步体征数据
 * @Author:  Hex (hex@zpdchina.com)
 * @Date:    2017-03-09 14:50:00
 * @version  $Id$
 */
class DataTizhengDataModel extends BaseModel
{

    //返回给终端的可执行sql语句的字段名称数组
    public $_field_arr = [
        'id',
        'patient_id',
        'zhuyuan_id',
        'jiancha_time',
        'jiancha_doctor_name',
        'jiancha_doctor_id',
        'jiancha_type',
        'jiancha_danwei',
        'jiancha_value',
        'jiancha_fangshi',
        'beizhu',
        'modify_time',
        'jiancha_shijian_duan',
        'batch_id',
        'input_type',
        'data_source',
        'biaodan_type'

    ];
    public $_field_arr1 = [
        'id',
        'patient_id',
        'zhuyuan_id',
        'jiancha_time',
        'jiancha_doctor_name',
        'jiancha_doctor_id',
        'jiancha_type',
        'jiancha_danwei',
        'jiancha_value',
        'jiancha_fangshi',
        'beizhu',
        'modify_time',
        'jiancha_shijian_duan',
        'batch_id',
        'input_type',
        'data_source',
        'biaodan_type',
        'data_type',

    ];

    public function getTizhengData($zhuyuan_id,$user_department,$data,$modify_time = '')
    {
        $field_str = implode(',', $this->_field_arr);
        $field_str .= ",'上门' data_type";
        $query = DB::select($field_str)
            ->from('yuanwai_tizheng')
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->where('state', '=', '1');
        if($modify_time)
            $query->where('modify_time', '>=', $modify_time);
        if (!empty($data))
            $query->where('id', 'NOT IN', $data);
        $zhuayun_bingren = CommonModel::getZhuyuanIds($user_department,$this->yiyuan_id,$zhuyuan_id);
        if($zhuayun_bingren)
            $query->where('zhuyuan_id', 'IN', $zhuayun_bingren);
        $tizheng_data = $query ->execute()->as_array();

        return $tizheng_data;
    }

    public function getTizhengDatayuannai($zhuyuan_id,$user_department,$data,$modify_time = '')
    {
        $field_str = implode(',', $this->_field_arr);
        $field_str .= ",'电话' data_type";
        $query = DB::select($field_str)
            ->from('zhuyuan_tizheng')
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->where('state', '=', '1');
        if($modify_time)
            $query->where('modify_time', '>=', $modify_time);
        if (!empty($data))
            $query->where('id', 'NOT IN', $data);
        $zhuayun_bingren = CommonModel::getZhuyuanIds($user_department,$this->yiyuan_id,$zhuyuan_id);
        if($zhuayun_bingren)
            $query->where('zhuyuan_id', 'IN', $zhuayun_bingren);
        $tizheng_data = $query ->execute()->as_array();

        return $tizheng_data;
    }
    
    public function UpdateTizhengData($data=[]){
    	$data['modify_time'] = date('Y-m-d H:i:s',time());
    	$sql = DB::update('zhuyuan_tizheng')
    			->set($data)
    			->where('id', '=', trim($one['id']))
    			->where('state', '=', 1)
    			->where('yiyuan_id', '=', $this->yiyuan_id);
    	return $sql->execute();
    }

}